package com.qike.duola.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qike.duola.domain.*;
import com.qike.duola.dto.*;
import com.qike.duola.enums.*;
import com.qike.duola.mapper.DuolaCarMapper;
import com.qike.duola.mapper.DuolaCarRemainMapper;
import com.qike.duola.mapper.SysUserMapper;
import com.qike.duola.service.DuolaCarService;
import com.qike.duola.vo.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Eric.zhou
 * @Date: 2020/9/2 15:10
 */
@Service
public class DuolaCarServiceImpl implements DuolaCarService {
    @Autowired
    private DuolaCarMapper duolaCarMapper;
    @Autowired
    private DuolaCarRemainMapper duolaCarRemainMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 添加车辆
     *
     * @param carAddDto
     */
    @Override
    public void carAdd(CarAddDto carAddDto) throws Exception {
        Long userId = (Long) SecurityUtils.getSubject().getPrincipal();
        DuolaCarExample example = new DuolaCarExample();
        example.createCriteria().andCarNoEqualTo(carAddDto.getCarNo()).andStatusEqualTo(StatusEnums.NO.getEcode());
        List<DuolaCar> duolaCars = duolaCarMapper.selectByExample(example);
        if (duolaCars.size()>0){
            throw new Exception(ErrorEnums.SAME_CAR_NO.getEmsg());
        }
        DuolaCar car = new DuolaCar();
        car.setAddId(userId);
        System.out.println("当前时间"+ LocalDateTime.now());
        car.setAddTime(LocalDateTime.now());
        car.setCarNo(carAddDto.getCarNo());
        car.setCarStatus(carAddDto.getCarStatus());
        car.setCarType(carAddDto.getCarType());
        car.setDriverId(carAddDto.getDriverId());
        car.setDriverLicense(carAddDto.getDriverLicense());
        car.setDrivingLicense(carAddDto.getDrivingLicense());
        car.setSimNo(carAddDto.getSimNo());
        car.setStatus(StatusEnums.NO.getEcode());
        car.setIsOut(carAddDto.getIsOut());
        car.setTransStatus(CarTransStatusEnums.MIX_CAR.getEcode());
        car.setCapacity(carAddDto.getCarModel());
        duolaCarMapper.insertSelective(car);
        //余料 默认=0
        DuolaCarRemain duolaCarRemain = new DuolaCarRemain();
        duolaCarRemain.setCarId(car.getId());
        duolaCarRemain.setRemain(new Double(0));
        duolaCarRemainMapper.insertSelective(duolaCarRemain);
        //redis里添加车辆信息(key=设备号,value=车牌号)
        redisTemplate.opsForValue().set(carAddDto.getDeviceNo(), JSON.toJSONString(car));


    }

    /**
     * 编辑车辆
     *
     * @param carEditDto
     */
    @Override
    public void carEdit(CarEditDto carEditDto) throws Exception {
        Long userId = (Long) SecurityUtils.getSubject().getPrincipal();
        DuolaCar car = duolaCarMapper.selectByPrimaryKey(carEditDto.getId());
        if (car==null){
            throw new Exception(ErrorEnums.ERROR_DATA.getEmsg());
        }
        car.setUpdateId(userId);
        car.setUpdateTime(LocalDateTime.now());
        car.setCarNo(carEditDto.getCarNo());
        car.setCarStatus(carEditDto.getCarStatus());
        car.setCarType(carEditDto.getCarType());
        car.setDriverId(carEditDto.getDriverId());
        car.setDriverLicense(carEditDto.getDriverLicense());
        car.setDrivingLicense(carEditDto.getDrivingLicense());
        car.setSimNo(carEditDto.getSimNo());
        car.setIsOut(carEditDto.getIsOut());
        car.setCapacity(carEditDto.getCarModel());
        duolaCarMapper.updateByPrimaryKeySelective(car);
    }

    /**
     * 删除车辆
     *
     * @param deptDelDto
     */
    @Override
    public void carDel(DeptDelDto deptDelDto) throws Exception {
        Long userId = (Long) SecurityUtils.getSubject().getPrincipal();
        DuolaCar car = duolaCarMapper.selectByPrimaryKey(deptDelDto.getId());
        if (car==null){
            throw new Exception(ErrorEnums.ERROR_DATA.getEmsg());
        }
        car.setUpdateId(userId);
        car.setUpdateTime(LocalDateTime.now());
        car.setStatus(StatusEnums.YES.getEcode());
        duolaCarMapper.updateByPrimaryKeySelective(car);
    }

    /**
     * 车辆列表
     *
     * @param carListDto
     * @return
     */
    @Override
    public CarListVo carList(CarListDto carListDto) {
        CarListVo vo = new CarListVo();
         List<CarList> list = new ArrayList<>();
        Long userId = (Long) SecurityUtils.getSubject().getPrincipal();
        DuolaCarExample example = new DuolaCarExample();
        DuolaCarExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(carListDto.getCarNo())){
            criteria.andCarNoLike(carListDto.getCarNo());
        }
        if (carListDto.getCarType()!=null){
            criteria.andCarTypeEqualTo(carListDto.getCarType());
        }
        criteria.andStatusEqualTo(StatusEnums.NO.getEcode());
        example.setOrderByClause("add_time desc" );
        Page page = PageHelper.startPage(carListDto.getStartPage(), carListDto.getPageSize());
        List<DuolaCar> duolaCars = duolaCarMapper.selectByExample(example);
        for (DuolaCar duolaCar : duolaCars) {
            CarList carList = new CarList();
            carList.setId(duolaCar.getId());
            if (duolaCar.getCapacity()!=null){
                carList.setCarModel(duolaCar.getCapacity().toString());
            }
            carList.setCarType(CarTypeEnums.statOf(duolaCar.getCarType()).getEmsg());
            carList.setCarStatus(IsOutEnums.statOf(duolaCar.getCarStatus()).getEmsg());
            if (duolaCar.getDriverId()!=null){
                SysUserWithBLOBs sysUserWithBLOBs = userMapper.selectByPrimaryKey(duolaCar.getDriverId());
                if (sysUserWithBLOBs!=null){
                    carList.setDriverName(sysUserWithBLOBs.getNickname());
                    carList.setPhone(sysUserWithBLOBs.getMobilePhone());
                }
            }
            carList.setSimNo(duolaCar.getSimNo());
            if (duolaCar.getCapacity()!=null){
                carList.setCarModel(duolaCar.getCapacity().toString());
            }
            carList.setCarNo(duolaCar.getCarNo());
            carList.setIsOut(CarStatusEnums.statOf(duolaCar.getIsOut()).getEmsg());
            list.add(carList);
        }
        vo.setList(list);
        vo.setTotal(page.getTotal());
        return vo;
    }

    /**
     * 车辆详情
     *
     * @param id
     * @return
     */
    @Override
    public CarInfoVo carInfo(Integer id) throws Exception {
        CarInfoVo vo = new CarInfoVo();
        DuolaCar car = duolaCarMapper.selectByPrimaryKey(id);
        if (car==null){
            throw new Exception(ErrorEnums.ERROR_DATA.getEmsg());
        }
        if (car.getCapacity()!=null){
            vo.setCarModel(car.getCapacity().toString());
        }
        vo.setCarNo(car.getCarNo());
        vo.setCarStatus(car.getCarStatus());
        if (car.getCapacity()!=null){
            vo.setCarModel(car.getCapacity().toString());
        }
        vo.setDriverId(car.getDriverId());
        vo.setSimNo(car.getSimNo());
        vo.setIsOut(car.getIsOut());
        vo.setCarType(car.getCarType());
        vo.setDeviceNo(car.getDeviceId());
        vo.setDriverId(car.getDriverId());
        if (car.getDriverId()!=null){
            SysUserWithBLOBs sysUserWithBLOBs = userMapper.selectByPrimaryKey(car.getDriverId());
            vo.setDriverName(sysUserWithBLOBs.getNickname());
            vo.setPhone(sysUserWithBLOBs.getMobilePhone());
        }
        vo.setDriverLicense(car.getDriverLicense());
        vo.setDrivingLicense(car.getDrivingLicense());
        return vo;
    }

    /**
     * 根据车牌号和类型下拉车辆
     *
     * @param type
     * @param carNo
     * @param volume
     * @return
     */
    @Override
    public List<CarPullByTypeOrNo> carPullByTypeOrNo(Integer type, String carNo, Double volume) {
        List<CarPullByTypeOrNo> list = new ArrayList<>();
        DuolaCarExample example = new DuolaCarExample();
        DuolaCarExample.Criteria criteria = example.createCriteria();
        if (type!=null){
            criteria.andCarTypeEqualTo(type);
        }
        if (!StringUtils.isEmpty(carNo)){
            criteria.andCarNoLike(carNo);
        }
        if (volume!=null){
            criteria.andCapacityGreaterThanOrEqualTo(volume);
        }
        List<DuolaCar> duolaCars = duolaCarMapper.selectByExample(example);
        for (DuolaCar duolaCar : duolaCars) {
            CarPullByTypeOrNo carPullByTypeOrNo = new CarPullByTypeOrNo();
            carPullByTypeOrNo.setCarNo(duolaCar.getCarNo());
            carPullByTypeOrNo.setId(duolaCar.getId());
            list.add(carPullByTypeOrNo);
        }
        return list;
    }

    /**
     * 获取所有车辆经纬度和方向
     *
     * @return
     */
    @Override
    public List<DuolaCar> getAllCar(Integer id) {
        DuolaCarExample carExample = new DuolaCarExample();
        if (id!=null){
            carExample.createCriteria().andIdEqualTo(id);
        }
        List<DuolaCar> duolaCars = duolaCarMapper.selectByExample(carExample);
        return duolaCars;
    }
}
